package com.uubx.service.impl;

import com.uubx.mapper.PermissionMapper;
import com.uubx.mapper.RoleMapper;
import com.uubx.mapper.UserMapper;
import com.uubx.pojo.Permission;
import com.uubx.pojo.Role;
import com.uubx.pojo.User;
import com.uubx.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author wuguojun
 * @Date 2020/12/26 18:53
 * @Version 1.0
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PermissionMapper permissionMapper;
    @Override
    public List<Role> queryAllRole(Integer startIndex, Integer limit) {
        List<Role> roles = roleMapper.queryAllRole(startIndex, limit);
        return roles;
    }

    @Override
    public Long queryAllRoleCount() {
        Long count = roleMapper.queryAllRoleCount();
        return count;
    }

    @Override
    public Role queryAllRoleByName(String roleName) {
        Role role = roleMapper.queryAllRoleByName(roleName);
        return role;
    }

    @Override
    public List<Role> queryRoleByStatus(Integer status) {
        List<Role> roles = roleMapper.queryRoleByStatus(status);
        return roles;
    }

    @Override
    public List queryAllRoleName() {
        List list = roleMapper.queryAllRoleName();
        return list;
    }

    @Override
    public List<User> queryAllUsersByRoleId(Integer roleId) {
        List<User> users = userMapper.queryAllUsersByRoleId(roleId);
        return users;
    }

    @Override
    public Long queryAllUsersByRoleIdToCount(Integer roleId) {
        Long count = userMapper.queryAllUsersByRoleIdToCount(roleId);
        return count;
    }

    @Override
    public List<Permission> queryAllPermission() {
        List<Permission> permissions = permissionMapper.queryAllPermission();
        return permissions;
    }

    @Override
    public List<Permission> queryPermissionByRoleId(Integer role_id) {
        List<Permission> permissions = permissionMapper.queryPermissionByRoleId(role_id);
        return permissions;
    }

    @Override
    public void deleteAllPermissionByRoleId(Integer role_id) {
        permissionMapper.deleteAllPermissionByRoleId(role_id);
    }

    @Override
    public void addRolePermissionByRoleId(Integer roleId, Integer permissionId) {
        permissionMapper.addRolePermissionByRoleId(roleId,permissionId);
    }

    @Override
    public List<Permission> queryPermissionByRoleName(String roleName) {
        List<Permission> permissions = permissionMapper.queryPermissionByRoleName(roleName);
        return permissions;
    }

    @Override
    public void deleteRole(Integer id) {
        roleMapper.deleteRole(id);
        roleMapper.deleteRoleAndPermissions(id);
    }

    @Override
    public void updateRoleByStatus(Role role) {
        if (role.getStatus()==1){
            role.setStatus(0);
            roleMapper.updateRoleByStatus(role);
        }else{
            role.setStatus(1);
            roleMapper.updateRoleByStatus(role);
        }

    }

    @Override
    public void addRole(Role role) {
        role.setDescription(null);
        role.setStatus(0);
        role.setCreateTime(new Date());
        role.setUpdateTime(new Date());
        roleMapper.addRole(role);
    }
}
